﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Net;
using System.IO;

namespace SilverConversion
{
    public static class Common
    {
        public static string GetPrice(GoodsType typ, System.Text.Encoding encoding, out string errInfo)
        {
            errInfo = string.Empty;
            string interfaceUrl = GetInterfaceUrl(typ);
            try
            {
                WebRequest request = WebRequest.Create(interfaceUrl);
                request.UseDefaultCredentials = false;

                WebResponse response = request.GetResponse();
                Stream resStream = response.GetResponseStream();
                StreamReader reader = new StreamReader(resStream, encoding);
                string price_string = reader.ReadToEnd();
              
                reader.Close();
                resStream.Close();

                return GetPriceCombo(typ, price_string);
            }
            catch (WebException wex)
            {
                return string.Empty;
            }
            catch (Exception e)
            {
                errInfo = e.Message;
                return string.Empty;
            }
        }

        public static string GetPriceCombo(GoodsType typ, string price_string)
        {
            string price_current = "", price_highest = "", price_lowest = "";
            if (typ == GoodsType.juntai)
            {
                if (!string.IsNullOrEmpty(price_string))
                {
                    string dataString = price_string.Split(':')[2].Replace("]}]}", "");
                    price_current = dataString.Split(',')[2].Trim('\"');
                    price_highest = dataString.Split(',')[5].Trim('\"');
                    price_lowest = dataString.Split(',')[6].Trim('\"');
                }
            }
            if (typ == GoodsType.tiantong)
            {
                if (!string.IsNullOrEmpty(price_string))
                {
                    int startIndex = price_string.IndexOf("现货白银</td>") + 9;
                    int endIndex = price_string.IndexOf("</tr></tbody>");
                    string dataString = price_string.Substring(startIndex, endIndex - startIndex);
                    string regRule = "<td[^>]*?>(.*?)</td>";
                    MatchCollection mc = Regex.Matches(dataString, regRule, RegexOptions.IgnoreCase);
                    string regReplace = @"(?i)[\<]td.*?[\>]";    //将所有<td......> 替换掉

                    price_current = Regex.Replace(mc[0].Value, regReplace, "").Replace("</td>", "");
                    price_highest = Regex.Replace(mc[3].Value, regReplace, "").Replace("</td>", "");
                    price_lowest = Regex.Replace(mc[4].Value, regReplace, "").Replace("</td>", "");
                }
            }
            if (typ == GoodsType.us)
            {
                if (!string.IsNullOrEmpty(price_string))
                {
                    int startIndex = price_string.IndexOf("[") + 1;
                    int endIndex = price_string.IndexOf("]");
                    price_string = price_string.Substring(startIndex, endIndex - startIndex);

                    price_current = price_string.Split(',')[2].Trim().Trim('\'');
                    price_highest = price_string.Split(',')[7].Trim().Trim('\'');
                    price_lowest = price_string.Split(',')[8].Trim().Trim('\'');
                }
            }
            return price_current + "|" + price_highest + "|" + price_lowest;
        }

        public static string GetInterfaceUrl(GoodsType typ)
        {
            string url = "";
            Random rd = new Random();
            string rmd1 = rd.Next(100000, 999999).ToString();
            string rmd2 = rd.Next(100000, 999999).ToString();

            if ((int)typ == 1)
            {
                url = "http://quote.forex.hexun.com/2010/Data/FRunTimeQuote.ashx?code=XAGUSD";
            }
            else if ((int)typ == 2)
            {
                url = "http://www.tjpme.com/mytest.php?rand=" + rmd1;
            }
            else if ((int)typ == 3)
            {
                url = "http://livery.kiskey.com:8088/ajaxhandler/dataupdate.ashx";

                url += "?" + rmd1 + "=" + rmd2 + "&flag=8";
            }
            return url;
        }

        /// <summary>
        /// 白银类别
        /// </summary>
        public enum GoodsType
        {
            us = 1,
            tiantong = 2,
            juntai = 3
        }

        public enum HighPriceAlertLevel
        {
            H1 = 1,
            H2 = 2,
            H3 = 3
        }
        public enum LowPriceAlertLevel
        {
            L1 = 1,
            L2 = 2,
            L3 = 3
        }

    }
}
